package com.example.shouYeService.siYunShiPinTongJi.shiXianLei;

import com.example.shouYeDao.siYunShouYeDao.CourseStatistics;
import com.example.shouYePoji.siYunShiPinTongJi.*;
import com.example.shouYeService.siYunShiPinTongJi.CourseStatisticsServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 首页
 * 课程增长统计
 * 2022 11-13
 */
@Service
public class CourseStatisticsServce implements CourseStatisticsServcie {
    @Autowired
    private CourseStatistics ju;

    //专门算除法
    public static String Chufa(long a,long b) {
        //“0.00000000”确定精度
        DecimalFormat dF = new DecimalFormat("0.00000000");
        return dF.format((float)a/b);
    }
    @Override
    public  CourseReturnValue CourseStatisticsJiYiZhou() {
        List<CourseStatisticsS> courseStatisticsOne = ju.CourseStatisticsJiYiZhou();
        CourseReturnValue l = new CourseReturnValue();
        String[] a = new String[7];
        long[] b = new long[7];
        int js = 0;
        for (CourseStatisticsS ab:courseStatisticsOne){
            if (js==0){
                a[0]="星期一";
            }else if (js==1){
                a[1]="星期二";
            }else if (js==2){
                a[2]="星期三";
            }else if (js==3){
                a[3]="星期四";
            }else if (js==4){
                a[4]="星期五";
            }else if (js==5){
                a[5]="星期六";
            }else if (js==6){
                a[6]="星期日";
            }
            b[js]= ab.getValue();
            js++;
        }
        l.setKey(a);
        l.setValue(b);
        return l;
    }

    @Override
    public CourseReturnValue CourseStatisticsLiangGeShiJianZhiJian(BetweenTwoTimes k) {
        Long timeStamp = k.getDateTime01().getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyy-MM-dd");
        String DateTime01 = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));

        Long timeStamp02 = k.getDateTime02().getTime();
        String DateTime02 = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp02))));

        List<CourseStatisticsS> gh = ju.CourseStatisticsLiangGeShiJianZhiJian(DateTime01,DateTime02);
        int size = gh.size();
        String[] a = new String[size];
        long[] b = new long[size];
        int sy=0;
        for (CourseStatisticsS gr:gh){
            Long timeStampp = gr.getDays().getTime();
            SimpleDateFormat sdff=new SimpleDateFormat("MM月dd日");
            String sd = sdff.format(new Date(Long.parseLong(String.valueOf(timeStampp))));
            a[sy]=sd;
            b[sy]=gr.getValue();
            sy++;
        }
        CourseReturnValue hy = new CourseReturnValue();
        hy.setKey(a);
        hy.setValue(b);
        return hy;
    }
//首页上周新增总量
// 上周和上上周的同比率
    @Override
    public CourseStatistics_compareSmallModules YearOnYear() {
        CourseStatistics_compareSmallModules zz = new CourseStatistics_compareSmallModules();
        CourseStatistics_compareSmallModules upperWeek = ju.getUpperWeek(CalculateLastWeekDate.szy(),CalculateLastWeekDate.szr());
        long b=upperWeek.getValue();
        zz.setValue(b);
        CourseStatistics_compareSmallModules upperWeek2 = ju.getUpperWeek(CalculateLastWeekDate.sszy(),CalculateLastWeekDate.sszr());
        long bb=upperWeek2.getValue();
        if (bb!=0&&b!=bb){
            double l= Double.parseDouble(Chufa(b-bb,bb))*100;
            long lp = (long) l;

            zz.setYoyRate(lp);
            return zz;
        }else {
            zz.setYoyRate(0);
            return zz;
        }


    }

    @Override
    public CourseStatistics_compareSmallModules YearOnYear02() {
        //创建对象，最终将这个对象返回给控制层
        CourseStatistics_compareSmallModules jg = new CourseStatistics_compareSmallModules();
        //首先算,上周的课程申请通过率
        CourseStatistics_compareSmallModules upperWeek = ju.getUpperWeek(CalculateLastWeekDate.szy(),CalculateLastWeekDate.szr());
        long b=upperWeek.getValue();
        CourseStatistics_compareSmallModules upperWeek2 = ju.getAllApplications(CalculateLastWeekDate.szy(),CalculateLastWeekDate.szr());
        long bb=upperWeek2.getValue();
        long d11 = 0;
        if (bb!=0&&bb!=b){
            double d1 = Double.parseDouble(Chufa(b-bb,bb))*100;
            d11 = (long) d1;
        }

        //添加第一个属性
        jg.setValue(d11);
        //再算上周与上上周通过率的同比率
        CourseStatistics_compareSmallModules upperWeek01 = ju.getUpperWeek(CalculateLastWeekDate.sszy(),CalculateLastWeekDate.sszr());
        long a=upperWeek01.getValue();
        CourseStatistics_compareSmallModules upperWeek02 = ju.getAllApplications(CalculateLastWeekDate.sszy(),CalculateLastWeekDate.sszr());
        long aa=upperWeek02.getValue();
        long a11 = 0;
        if (aa!=0&&a!=aa){
            double a1 = Double.parseDouble(Chufa(a-aa,aa))*100;
            a11 = (long) a1;
        }

        //同比率的计算
        long tbb=0;
        if (a11!=0&&d11!=a11){
            double tb = Double.parseDouble(Chufa(d11-a11,a11))*100;
            tbb = (long) tb;
        }

        //将同比率添加到属性
        jg.setYoyRate(tbb);
        return jg;
    }
}
